package fr.grobim.hangovers.gestionBudget.intercepteurs.comptes;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

import fr.grobim.hangovers.gestionBudget.actions.comptes.ConnexionAction;
import fr.grobim.hangovers.gestionBudget.actions.enums.AttributsSession;
import fr.grobim.hangovers.gestionBudget.actions.support.AbstractActionSupport;

public class VerifieConnexionIntercepteur implements Interceptor {

	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String retourAction;
		
		AbstractActionSupport actionInterceptee = (AbstractActionSupport) invocation.getAction();
		
		if (actionInterceptee instanceof ConnexionAction) {
			retourAction = invocation.invoke();
		} else {
			Map<String, Object> session = invocation.getInvocationContext().getSession();
			if (session.containsKey(AttributsSession.MEMBRE_CONNECTE.getClee())) {
				retourAction = invocation.invoke();
			} else {
				retourAction = AbstractActionSupport.LOGIN;
			}
		}
		return retourAction;
	}

	@Override
	public void destroy() {}

	@Override
	public void init() {}

}
